package com.tx365.db;

import java.util.Calendar;
import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import com.tx365.db.CallLogs.CallLogColumns;

public class DataBaseProvider  {
    
	private static final String TAG = "DataBaseProvider";

	private static final String DATABASE_NAME = "Tx365.db";
	private static final int    DATABASE_VERSION = 1;

	private static final String CONTACT_TABLE_NAME ="CallLogs";
	private static HashMap<String, String> sRSSProjectionMap;
	private static UriMatcher mUriMatcher;
	
	private static final String CREATE_TABLE = "";
	 
	 

	private static final int CALLLOGS = 1;
	private static final int CALLLOGS_ID = 2;

       

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public  DatabaseHelper(Context dataBaseProvider) {
			super(dataBaseProvider, DATABASE_NAME, null, DATABASE_VERSION);
			Log.i("jinyan", "DATABASE_VERSION=" + DATABASE_VERSION);
			System.out.println("d1");
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.i("jinyan", "onCreate(SQLiteDatabase db)");
			String sql = "CREATE TABLE " + CONTACT_TABLE_NAME + " ("
					+ CallLogColumns._ID + " INTEGER PRIMARY KEY,"
					+ CallLogColumns.CALLERTEL + " TEXT," + CallLogColumns.CALLEDTEL
					+ " TEXT," + CallLogColumns.CALLTIME + " TEXT"+ ");";
			System.out.println(sql); 
			Log.i("jinyan", "sql="+sql);
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.i("jinyan",
					" onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)="
							+ newVersion);
			db.execSQL("DROP TABLE IF EXISTS " + CONTACT_TABLE_NAME);
			onCreate(db);
		}
	}
	
	private DatabaseHelper mOpenHelper; 

	public DataBaseProvider() {
	    super();
	    // TODO Auto-generated constructor stub
	    
	}
	public DataBaseProvider(Context context) {
	   
	    // TODO Auto-generated constructor stub
	    mOpenHelper = new DatabaseHelper(context);
	}
	public Cursor showItems() {

		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		String col[] = {CallLogColumns._ID, CallLogColumns.CALLEDTEL, CallLogColumns.CALLTIME };
		Cursor cur = db.query(CONTACT_TABLE_NAME, col, null, null, null, null, CallLogColumns.CALLTIME + " DESC ");
		return cur; 
	}
	 
	public  void insertItems( ContentValues initialValues) {
	    SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 
	    
	    try {
		String sql1 = "insert into " + CONTACT_TABLE_NAME + " (" + CallLogColumns.CALLEDTEL + ", " + CallLogColumns.CALLTIME + ", " + CallLogColumns.CALLERTEL
			+ ") values('"+ initialValues.getAsString(CallLogColumns.CALLEDTEL) + "','"+ getFormateCreatedDate() + "','"+ initialValues.getAsString(CallLogColumns.CALLERTEL)  +"');";
		db.execSQL(sql1);
		System.out.println(sql1);
		
	    } catch (SQLException e) {
		
	    }		 
	}

	public void deleteItem(String strId)
	{
	    SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 
	    
	    try {
		String sql1 = "delete from " + CONTACT_TABLE_NAME + " where _id = " + strId;
		db.execSQL(sql1);
		System.out.println(sql1);
		
	    } catch (SQLException e) {
		
	    }
	}

	public static String getFormateCreatedDate() {
		Calendar calendar = Calendar.getInstance();
		String created = calendar.get(Calendar.YEAR) + "-"
				+ calendar.get(Calendar.MONTH) + "-"
				+ calendar.get(Calendar.DAY_OF_MONTH) 
				+" "
				+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
				+ calendar.get(Calendar.MINUTE) + ":"
				+ calendar.get(Calendar.SECOND) ;
		return created;
	}

	 
	
}
